import { NgFor, ViewportScroller } from '@angular/common';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Component, OnInit, Output, signal } from '@angular/core';
import { ToastService } from 'ngx-metro';
import { Result } from '../common/bean/result-bean';
import { ArticleListVo } from './vo/article-list-vo';
import { InfoBoxComponent } from './widget/info-box/info-box.component';
import { ArticleCardComponent } from './widget/article-card/article-card.component';

@Component({
    selector: 'app-blog',
    templateUrl: './blog.component.html',
    styleUrls: ['./blog.component.scss'],
    standalone: false,
})
export class BlogComponent implements OnInit {

  articleList: Array<ArticleListVo>

  page=1;
  size=5;

  constructor(private http: HttpClient,
    private viewportScroller :ViewportScroller,
    private toastService: ToastService
    ) {}

  ngOnInit(): void {
    this.getArticleList(1,5);
  }

  nextPage(){
    if(this.articleList.length>=this.size){
      this.page++;
      this.getArticleList(this.page,this.size);
      this.viewportScroller.scrollToPosition([0,0]);
    }

  }

  prePage(){
    if(this.page>1){
      this.page--;
      this.getArticleList(this.page,this.size);
      this.viewportScroller.scrollToPosition([0,0]);
    }else{
      this.toastService.create('Here is the first page', { additional: { distance: 20, showTop: true }, cls: 'alert', timeout: 1000 });
    }
  }


  getArticleList(page: number, size:number): void {
    this.http.get<Result<Array<ArticleListVo>>>('article/getArticleList?page='+page+'&size='+size).subscribe( response =>{
      // console.log(response.data);
      this.articleList = response.data;
    })
  }

}
